package com.stellar.circle.controller.miniApp.sPost;

import com.google.common.base.Preconditions;
import com.stellar.circle.controller.admin.sPost.vo.SPostRequestPageVO;
import com.stellar.circle.controller.admin.sPost.vo.SPostRequestVO;
import com.stellar.circle.controller.admin.sPost.vo.SPostResponseVO;
import com.stellar.circle.controller.miniApp.sPost.vo.PostPinRequestVO;
import com.stellar.circle.controller.miniApp.sPost.vo.TimeSlotPriceVO;
import com.stellar.circle.domain.sPost.SPostDomainService;
import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.bean.Result;
import com.stellar.web.core.constants.AuthUserConstants;
import com.stellar.web.core.validator.ValidatorRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 校园墙帖子表控制器
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-17 19:23:35
 */
@RequestMapping("/api-miniApp/sPost")
@RestController("miniAppSPostController")
@Slf4j
public class SPostController {

    @Resource
    private SPostDomainService sPostDomainService;

    /**
     * 分页查询校园墙帖子表
     */
    @PostMapping("/page")
    public Result<PageResult<SPostResponseVO>> page(@RequestBody SPostRequestPageVO requestPageVO) {
        try {

            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(requestPageVO, "请求参数不能为空");
                Preconditions.checkNotNull(requestPageVO.getSchoolId(), "学校Id不能为空");
            }

            return Result.success(sPostDomainService.page(requestPageVO));
        } catch (IllegalArgumentException iae) {
            log.error("SPostController.page.error:{}", iae.getMessage());
            return Result.fail(iae.getMessage());
        }
    }

    /**
     * 新增校园墙帖子表
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @PostMapping("/create")
    public Result<Boolean> create(@RequestBody SPostRequestVO requestVO) {
        try {

            if (log.isInfoEnabled()) {
                Preconditions.checkNotNull(requestVO, "请求参数不能为空");
                Preconditions.checkNotNull(requestVO.getContent(), "内容不能为空");
                Preconditions.checkNotNull(requestVO.getTitle(), "标题不能为空");
                Preconditions.checkNotNull(requestVO.getSchoolId(), "学校Id不能为空");
                Preconditions.checkNotNull(requestVO.getIsAnonymous(), "是否匿名不能为空");
            }

            return Result.success(sPostDomainService.insert(requestVO));
        } catch (IllegalArgumentException iae) {
            log.error("SPostController.create.error:{}", iae.getMessage());
            return Result.fail(iae.getMessage());
        }
    }

    /**
     * 修改校园墙帖子表
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SPostRequestVO requestVO) {
        return Result.success(sPostDomainService.update(requestVO));
    }

    /**
     * 删除校园墙帖子表
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(sPostDomainService.delete(id));
    }


    /**
     * 获取校园墙帖子表详情
     */
    @GetMapping("/get/{id}")
    public Result<SPostResponseVO> get(@PathVariable("id") Long id) {
        return Result.success(sPostDomainService.get(id));
    }

    /**
     * 增加浏览量
     * 当前逻辑前端自主判断：存储postId在本地缓存中，避免重复增加，本地缓存实效后当作没有浏览过，可以再次增加
     */
    @PostMapping("/view")
    public Result<Boolean> view(@RequestParam("postId") Long postId) {
        return Result.success(sPostDomainService.view(postId));
    }

    /**
     * 获取置顶时间段和价格
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @GetMapping("/pin/pricing")
    public Result<List<TimeSlotPriceVO>> getPinPricing() {
        return Result.success(sPostDomainService.getPinPricing());
    }

    /**
     * 获取置顶订单号
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @GetMapping("/pin/pinOrderNo")
    public Result<String> getPinOrderNo() {
        return Result.success(sPostDomainService.getPinOrderNo());
    }

    /**
     * 置顶帖子
     */
    @ValidatorRole(value = {AuthUserConstants.SUPER_ADMIN, AuthUserConstants.SCHOOL_ADMIN, AuthUserConstants.NORMAL_USER})
    @PostMapping("/pin/create")
    public Result<Boolean> createPostPin(@RequestBody PostPinRequestVO postPinRequestVO) {
        return Result.success(sPostDomainService.createPostPin(postPinRequestVO));
    }
}
